package com.mubolg.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.aliyun.dysmsapi20170525.Client;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mubolg.AppLoginFeignClient;
import com.mubolg.constants.Constants;
import com.mubolg.constants.GlobalExceptionCode;
import com.mubolg.mapper.DriverMapper;
import com.mubolg.pojo.domain.Driver;
import com.mubolg.pojo.domain.DriverSetting;
import com.mubolg.pojo.domain.DriverSummary;
import com.mubolg.pojo.domain.DriverWallet;
import com.mubolg.pojo.param.RegisterSaveLoginParam;
import com.mubolg.pojo.properties.AppSettingProperties;
import com.mubolg.pojo.properties.AppSmsSettingProperties;
import com.mubolg.pojo.properties.DriverSettingProperties;
import com.mubolg.service.IDriverService;
import com.mubolg.service.IDriverSettingService;
import com.mubolg.service.IDriverSummaryService;
import com.mubolg.service.IDriverWalletService;
import com.mubolg.template.AppWeChatTemplate;
import com.mubolg.utils.AssertUtil;
import com.mubolg.utils.BitStatesUtil;
import com.mubolg.utils.NameUtil;
import com.mubolg.utils.SMSUtils;
import java.math.BigDecimal;
import java.util.Date;
import org.ini4j.Reg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;



/**
 * <p>
 * 司机对象 服务实现类
 * </p>
 *
 * @author YuGeGe
 * @since 2024-07-28
 */
@Service
@Transactional
public class DriverServiceImpl extends ServiceImpl<DriverMapper, Driver> implements IDriverService {
    @Autowired
    private IDriverSettingService driverSettingService;
    @Autowired
    private IDriverSummaryService driverSummaryService;
    @Autowired
    private IDriverWalletService driverWalletService;
    @Autowired
    private AppSettingProperties appSettingProperties;
    @Autowired
    private DriverSettingProperties driverSettingProperties;
    @Autowired
    private AppSmsSettingProperties appSmsSettingProperties;
    @Autowired
    private AppWeChatTemplate appWeChatTemplate;
    @Autowired
    private AppLoginFeignClient appLoginFeignClient;
    @Override
    @Transactional
    public void register(String openIdCode , String phoneCode) throws Exception {
        //获取openid
        String openID = appWeChatTemplate.getOpenID(openIdCode);
        AssertUtil.isNotNull(openID, GlobalExceptionCode.PARAM_CODE_ERROR);
        //如果已经存在
        Driver driverByOpenId = getOne(new QueryWrapper<Driver>().eq("open_id", openID));
        AssertUtil.isNull(driverByOpenId, GlobalExceptionCode.PARAM_PHONE_EXIST);
        //获取手机号
        String phone = appWeChatTemplate.getPhone(phoneCode);
        //业务实现
        Driver driver = new Driver();
        //如果为空那么就执行下方向数据库添加信息
        //保存Driver信息
        saveDriver(driver,openID,phone);
        //生成随机密码
        String password = RandomUtil.randomNumbers(6);
        //生成随机名称
        String name = NameUtil.getName();
        //初始化从表
        initDriverAssociation(driver.getId(),password);
        //保存登录信息
        extracted(phone, password, name, openID);
        //发送验证码
        smsSend(phone,password);
    }
    /**
     * 保存Login
     */
    private void extracted(String phone, String password, String name, String openID) {
        //调用feign接口保存Login
        RegisterSaveLoginParam registerSaveLoginParam = new RegisterSaveLoginParam();
        registerSaveLoginParam.setUsername(phone);
        registerSaveLoginParam.setPassword(password);
        registerSaveLoginParam.setType(Constants.Login.TYPE_DRIVER);
        registerSaveLoginParam.setEnabled(true);
        registerSaveLoginParam.setAvatar(Constants.DEFAULT_HEAD_IMAGE);
        registerSaveLoginParam.setAdmin(false);
        registerSaveLoginParam.setNickName(name);
        registerSaveLoginParam.setOpenId(openID);
        registerSaveLoginParam.setPhone(phone);
        appLoginFeignClient.save(registerSaveLoginParam);
    }

    /**
     * 新增账号信息
     * @param driver
     * @param openId
     * @param phoneNumber
     */
    private void saveDriver(Driver driver,String openId,String phoneNumber) {
        driver.setPhone(phoneNumber);
        driver.setArchive(false);
        driver.setCreateTime(new Date());
        driver.setDeleted(false);
        driver.setVersion(0);
        driver.setOpenId(openId);
        driver.setBitState(BitStatesUtil.OP_PHONE);
        super.save(driver);
    }

    /**
     * 初始化从表数据
     * @param id
     * @param password
     */
    private void initDriverAssociation(Long id ,String password) {
        DriverSetting driverSetting = new DriverSetting();
        driverSetting.setId(id);
        driverSetting.setAutoAccept(false);
        driverSetting.setOrientation(false);
        driverSetting.setListenService(false);
        driverSetting.setOrderDistance(driverSettingProperties.getOrderDistance());
        driverSetting.setRangeDistance(driverSettingProperties.getRangeDistance());
        driverSetting.setCreateTime(new Date());
        driverSetting.setDeleted(false);
        driverSetting.setVersion(0);
        driverSettingService.save(driverSetting);


        DriverWallet driverWallet = new DriverWallet();
        driverWallet.setId(id);
        driverWallet.setAmount(new BigDecimal("0"));
        //随机生成密码，并且发送短信验证码告知用户 完成！
        driverWallet.setPassword(password);
        driverWallet.setDeleted(false);
        driverWallet.setVersion(0);
        driverWallet.setCreateTime(new Date());
        driverWalletService.save(driverWallet);


        DriverSummary driverSummary = new DriverSummary();
        driverSummary.setId(id);
        driverSummary.setLevel(0);
        driverSummary.setTotalOrders(0);
        driverSummary.setWeekOrders(0);
        driverSummary.setGoodComments(0);
        driverSummary.setAppeal(0);
        driverSummary.setTotalComplaint(0);
        driverSummary.setTodayTotalCancel(0);
        driverSummary.setDriveDuration(0);
        driverSummary.setTradeOrders(0);
        driverSummary.setTodayTradeOrders(0);
        driverSummary.setTodayComplaint(0);
        driverSummary.setTodayCancel(0);
        driverSummary.setTodayIncome(new BigDecimal("0"));
        driverSummary.setCreateTime(new Date());
        driverSummary.setDeleted(false);
        driverSummary.setVersion(0);
        driverSummaryService.save(driverSummary);
    }

    /**
     * 短信发送
     * @param phone
     * @param password
     * @throws Exception
     */
    public  void  smsSend (String phone , String password) throws Exception {
        //创建客户端
        Client client = SMSUtils.createClient(appSmsSettingProperties.getAccessKey(), appSmsSettingProperties.getSecret());
        //发送短信
        SMSUtils.sendMessage(appSmsSettingProperties.getSignname(), appSmsSettingProperties.getTcode(), phone, password,appSmsSettingProperties.getAccessKey(), appSmsSettingProperties.getSecret());
    }
}
